package com.showdoc.controller;

import com.showdoc.mapper.UserMapper;
import com.showdoc.pojo.User;
import com.showdoc.utils.R;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;
import sun.plugin2.util.SystemUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("user")
@EnableAutoConfiguration
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @GetMapping("/info")
    public R selectById(HttpServletRequest request) {
        Long uid = (Long) request.getSession().getAttribute("user");
        User user = userMapper.selectByUid(uid);
        return R.success(user);
    }

    @GetMapping("/all")
    public List<User> selectAll() {
        List<User> users = userMapper.selectAll();
        return users;
    }

    //用户注册
    @PostMapping("/register")
    public R register(@RequestBody  User user) {
        User userExist = userMapper.findByName(user.getUsername());
        if (user.getUsername().equals("")) {
            return R.error("用户名不能为空");
        } else if (user.getPassword().equals("")) {
            return R.error("密码不能为空");
        } else if (userExist != null) {
            return R.error("账号已经存在") ;
        } else {
            Timestamp time = new Timestamp(System.currentTimeMillis());
            user.setReg_time(time);
            user.setLast_login_time(time);
            userMapper.save(user);
//            if(userMapper.loginCheck(user.getUsername(),user.getPassword()) !=null) {
//                return R.success("注册成功！");
//            }
            return R.success("注册成功！");
        }
//      return R.error("注册失败，请重试...");
    }

    //用户登录
    @PostMapping("/login")
    public R<User> login(@RequestBody User temp,
                         HttpSession session
                        )

    {
         User user = userMapper.findByName(temp.getUsername());
         if(user != null){
             if(user.getPassword().equals(temp.getPassword())){
                 session.setAttribute("user",user.getUid());

                 return R.success(user);
             }else {
                 return R.error("密码错误，请重试");
             }
         }
         return R.error("登陆失败，请重试");
    }

    //用户退出
    @PostMapping("/logout")
    public String logout(HttpServletRequest request){
        request.getSession().removeAttribute("user");
        return "安全退出成功";
    }

    //修改密码
    @PostMapping("/revisePwd")
    public String revisePwd(String oldPwd,String newPwd,HttpServletRequest request){
        Long getId = (Long)request.getSession().getAttribute("user");
        User user = userMapper.selectByUid(getId);
        if(user.getPassword().equals(oldPwd)){
            userMapper.revisePwd(user.getUsername(),newPwd);
            return "修改成功";
        }
        return "原密码输入错误，请重试";
    }
}
